package nio.base;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class RandomAccessFileDemo {
	public static void main(String[] args) throws IOException {
		RandomAccessFile raf = new RandomAccessFile("d:\\test.txt", "rw");

		FileChannel inChannel = raf.getChannel();
		ByteBuffer buf = ByteBuffer.allocate(48);

		int bytesRead = inChannel.read(buf);
		System.out.println("Read " + bytesRead);
		buf.flip();

		while (buf.hasRemaining()) {
			System.out.print((char) buf.get());
		}

		buf.clear();

		bytesRead = inChannel.read(buf);

		raf.close();

	}
}
